﻿using DevExpress.Utils;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using RecruitStudents.Client.BLL;
using RecruitStudents.Utility;
using RecruitStudents.Utility.Helper;
using RecruitStudents.Utility.Json;
using RecruitStudents.Utility.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RecruitStudents.Client.Base
{
    public partial class Base_ExamTypeForm : BaseForm
    {
        ServiceBase service = new ServiceBase();
        TB_Sys_SpecialityLevelExamTypeMapping model = null;
        string SpecialityKindsNO = string.Empty;
        string ExamLevel = string.Empty;
        string ExamLevelName = string.Empty;
        string ExamTypeNo = string.Empty;
        public Base_ExamTypeForm(string strSpecialityLevelMappingID)
        {
            InitializeComponent();
            model = service.GetSpecialityLevelExamTypeMappingModel(strSpecialityLevelMappingID);
            if (model != null)
            {
                SpecialityKindsNO = model.SpecialityKindsNO;
                ExamLevel = model.ExamLevel.ToSafeString();
                ExamLevelName = model.ExamLevelName;
                ExamTypeNo = model.ExamTypeNo;
            }
            BindExamType();
            BindExamScoreTyoe();
            BindCalculateScore();
        }
        public Base_ExamTypeForm(string strSpecialityKindsNO, string strExamLevel, string strExamTypeNo, string strExamLevelName)
        {
            InitializeComponent();
            SpecialityKindsNO = strSpecialityKindsNO;
            ExamLevel = strExamLevel;
            ExamLevelName = strExamLevelName;
            ExamTypeNo = strExamTypeNo;
            BindExamType();
            BindExamScoreTyoe();
            BindCalculateScore();
        }

        private void BindExamScoreTyoe()
        {           
            DataTable dt = service.GetExamScoreTypeTable(SessionerHelper.SystemYear);
          
            gridControl2.DataSource = dt;

            if (model != null)
            {
                for (int i = 0; i < gridView2.Rows.Count; i++)
                {
                    if (gridView2.Rows[i].DataRow[0].ToSafeString() == model.ExamScoreTypeID)
                    {
                        gridView2.FocusedRowHandle = i;
                        gridView2.SelectRow(i);
                    }
                }
            }
        }

        private void BindCalculateScore()
        {         
            DataTable dt = service.GetExamCalculateTypeFormTable(SessionerHelper.SystemYear);
            if (dt != null)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    dr["CalculateScoreType"] = Utility.Const.ConstAppConfig.DicExamCalculateTypeFormType[dr["CalculateScoreType"].ToSafeString()];
                }
            }
            gridControl3.DataSource = dt;

            if (model != null)
            {
                for (int i = 0; i < gridView3.Rows.Count; i++)
                {
                    if (gridView3.Rows[i].DataRow[0].ToSafeString() == model.CalculateScoreID)
                    {
                        gridView3.FocusedRowHandle = i;
                        gridView3.SelectRow(i);
                    }
                }
            }
        }

        private void BindExamType()
        {
            DataTable dt = service.GetExamTypeTable("");
            gridControl1.DataSource = dt;
            if (model != null)
            {
                for (int i = 0; i < gridView1.Rows.Count; i++)
                {
                    if (gridView1.Rows[i].DataRow[0].ToSafeString() == model.ExamTypeNo)
                    {
                        gridView1.FocusedRowHandle = i;
                        gridView1.SelectRow(i);
                    }
                }
            }
        }

        private void simpleButton4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (model == null)
            {
                model = new TB_Sys_SpecialityLevelExamTypeMapping();
            }
            else
            {
                model.Attach();
            }
            model.CalculateScoreID = GetCalculateScoreID();
            model.ExamLevel = ExamLevel.ToSafeInt();
            model.ExamLevelName = ExamLevelName;
            model.ExamScoreTypeID = GetExamScoreTypeID();
            model.ExamTypeNo = GetExamTypeNo();
            model.ExamYear = SessionerHelper.SystemYear;
            model.SpecialityKindsNO = SpecialityKindsNO;

            if (service.SaveSpecialityLevelExamTypeMapping(model))
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private string GetCalculateScoreID()
        {
            string strSpecialityKindsNo_Detail = string.Empty;
            GridView rowsSelected = gridView3 as GridView;
            if (rowsSelected.Rows.Count > 0)
            {
                int[] inteurm = rowsSelected.GetSelectedRows();
                if (inteurm != null && inteurm.Length > 0)
                {
                    strSpecialityKindsNo_Detail = rowsSelected.Rows[inteurm[0]].DataRow.ItemArray[0].ToString();
                }
            }
            return strSpecialityKindsNo_Detail;
        }

        private string GetExamScoreTypeID()
        {
            string strSpecialityKindsNo_Detail = string.Empty;
            GridView rowsSelected = gridView2 as GridView;
            if (rowsSelected.Rows.Count > 0)
            {
                int[] inteurm = rowsSelected.GetSelectedRows();
                if (inteurm != null && inteurm.Length > 0)
                {
                    strSpecialityKindsNo_Detail = rowsSelected.Rows[inteurm[0]].DataRow.ItemArray[0].ToString();
                }
            }
            return strSpecialityKindsNo_Detail;
        }

        private string GetExamTypeNo()
        {
            string strSpecialityKindsNo_Detail = string.Empty;
            GridView rowsSelected = gridView1 as GridView;
            if (rowsSelected.Rows.Count > 0)
            {
                int[] inteurm = rowsSelected.GetSelectedRows();
                if (inteurm != null && inteurm.Length > 0)
                {
                    strSpecialityKindsNo_Detail = rowsSelected.Rows[inteurm[0]].DataRow.ItemArray[0].ToString();
                }
            }
            return strSpecialityKindsNo_Detail;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            string SpecialityKindsNO = GetExamTypeNo();
            if (SpecialityKindsNO.Length == 0)
            {
                DevHelper.ShowInfoMessageBox("请先选择删除项!");
                return;
            }
            service.DeleteExamType(SpecialityKindsNO);
            BindExamType();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Base_ExamTypeForm_Option option = new Base_ExamTypeForm_Option(string.Empty);
            if (option.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                BindExamType();
            }
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            if (gridView1.GetFocusedValue() != null)
            {
                GridView rows = sender as GridView;
                int[] inteurm = rows.GetSelectedRows();
                if (inteurm != null && inteurm.Length > 0)
                {
                    string SpecialityKindsNO = rows.Rows[inteurm[0]].DataRow.ItemArray[0].ToString();
                    Base_ExamTypeForm_Option option = new Base_ExamTypeForm_Option(SpecialityKindsNO);
                    if (option.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        BindExamType();
                    }
                }
            }
        }

        private void gridView2_MouseMove(object sender, MouseEventArgs e)
        {
            //类名空间：DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo 
            GridHitInfo hint = gridView2.CalcHitInfo(e.X, e.Y);

            if (
            (gridView2.RowCount > 0) && /*记录数大于0*/

            (hint.InRowCell) && /*有效的单元格*/

            (hint.Column.Caption == "评分方式名称")/*在指定的列显示Hint*/
            )
            {
                DataRow hintRow = gridView2.GetDataRow(hint.RowHandle);//取出当前资料行 

                string strExamScoreTypeID = hintRow["ExamScoreTypeID"].ToString();             
                DataTable dt = service.GetExamScoreTypeTable_Detail(strExamScoreTypeID);


                ToolTipControllerShowEventArgs aa = new ToolTipControllerShowEventArgs();
                aa.AllowHtmlText = DefaultBoolean.True;
                aa.Title = "评分项明细"; //HTML, 粗体 

                string strHtml = "";
                //strHtml = "评分项名称<br>";
                //if (dt != null)
                //{
                //    foreach (DataRow dr in dt.Rows)
                //    {
                //        strHtml += "评分项名称:" + dr["SpecialityLevelTypeItemName"].ToSafeString() +
                //            "    最小值:" + dr["MinScore"].ToSafeString() +
                //            "    最大值:" + dr["MaxScore"].ToSafeString() +
                //            "    权重比例:" + dr["Weight"].ToSafeString() + "<br>";
                //    }
                //}




                aa.ToolTip = strHtml;//断行 
                aa.ShowBeak = true;
                aa.Rounded = true;////圆角 
                aa.RoundRadius = 7;//圆角率 
                //aa.ToolTipType = ToolTipType.SuperTip;//超级样式，可多行或显示图标 
                aa.ToolTipType = ToolTipType.Standard;//标准样式，可显示鸟嘴。 
                aa.IconType = ToolTipIconType.Information;//消息图标 
                aa.IconSize = ToolTipIconSize.Small;//大图标 

                toolTipController1.ShowHint(aa);//显示Hint窗体 
            }
            else
            {
                toolTipController1.HideHint();
            }
        }
    }
}
